package eu.tolu.cableSpotter;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import eu.tolu.cableSpotter.R;
import eu.tolu.cableSpotter.help.SitesOverlay;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

public class GmapView extends MapActivity {
	public String cabAddr = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gmap_view);

		final Bundle extras = getIntent().getExtras();

		if (extras != null) {
			final TextView txCabName = (TextView) findViewById(R.id.tx_cable_name_gm);
			txCabName.setText(extras.getString("cableName"));
			cabAddr = extras.getString("address");
		}
		
	    Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
	    GeoPoint gp = null;
	    Address ad = null;
	    try {
	        List<Address> addresses = geoCoder.getFromLocationName(cabAddr, 1);
	        for(Address address : addresses){
	            gp = new GeoPoint((int)(address.getLatitude() * 1E6), (int)(address.getLongitude() * 1E6));
	            address.getAddressLine(1);
	            ad = address;
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    }
		
		MapView myMapView = (MapView) findViewById(R.id.mapview);
		MapController myMapController = myMapView.getController();
		if(gp!=null){
			System.out.println(gp.getLatitudeE6()* 1E6+"--"+gp.getLongitudeE6()* 1E6);
			myMapController.setCenter(gp);
			myMapController.setZoom(16);
			
			List<Overlay> listOfOverlays;
	        Drawable drawable1 = this.getResources().getDrawable(R.drawable.waterskiicon);
	        SitesOverlay myitemizedoverlay = new SitesOverlay(drawable1, this, null);
	        OverlayItem myoverlayitem = new OverlayItem(gp, "Error!", "Cable not found");
	        myitemizedoverlay.addOverlay(myoverlayitem);
			
			listOfOverlays = myMapView.getOverlays();
			listOfOverlays.clear();
			listOfOverlays.add(myitemizedoverlay);
			
			myMapView.invalidate();
		}else {
			System.out.println("GP NULL");
		}
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}

//class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
//    ImageView bmImage;
//
//    public DownloadImageTask(ImageView bmImage) {
//        this.bmImage = bmImage;
//    }
//
//    protected Bitmap doInBackground(String... urls) {
//        String urldisplay = urls[0];
//        Bitmap mIcon11 = null;
//        try {
//            InputStream in = new java.net.URL(urldisplay).openStream();
//            mIcon11 = BitmapFactory.decodeStream(in);
//        } catch (Exception e) {
//            Log.e("Error", e.getMessage());
//            e.printStackTrace();
//        }
//        return mIcon11;
//    }
//
//    protected void onPostExecute(Bitmap result) {
//        bmImage.setImageBitmap(result);
//    }
//}
